package com.imoblife.brainwavestus.util;

import java.util.ArrayList;

import com.imoblife.brainwavestus.database.SQLiteManager;
import com.imoblife.brainwavestus.database.SQLiteManager.DownTaskTable;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.newdownloadframe.DownLoadTaskInfo;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItem;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItemPragrame;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.product.TapJoyInfo;
import com.imoblife.brainwavestus.product.Wave;
import com.imoblife.brainwavestus.product.WaveCollection;
import com.imoblife.brainwavestus.user.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBUtil {
	
	public static final String TAG = "database";
	public static final String DATABASE_NAME = "BrainWaveDB";
	
	//product table
	public static final String PRODUCT_TB_NAME = "ProductTable";
	public static final String PRODUCT_ID = "_id";
	public static final String PRODUCT_NAME = "name";
	public static final String PRODUCT_CATEGORY = "category";
	public static final String PRODUCT_IS_COLLECTION = "isProductCollection";
	public static final String PRODUCT_TITLE = "title";
	public static final String PRODUCT_FILE_SIZE = "size";
	public static final String PRODUCT_VERSION = "version";
	public static final String PRODUCT_DOWNLOAD_STATE = "state";
	public static final String PRODUCT_PARENT = "parent";
	public static final String PRODUCT_LEVEL = "level";
	public static final String PRODUCT_CHANGE_TIME = "changetime";
	public static final String PRODUCT_OLD_PRICE = "oldPrice";
	public static final String PRODUCT_PRICE_OF_DOLLAR = "discountPriceOfDollar";
	public static final String PRODUCT_PRICE_OF_GOLD = "discountPriceOfGold";
	public static final String PRODUCT_DISCOUNT = "discount";
	public static final String PRODUCT_LONG_DESCRIPTION = "longDescription";
	public static final String PRODUCT_SHORT_DESCRIPTION = "shortDescription";
	public static final String PRODUCT_HZ = "hz";
	public static final String PRODUCT_DETAIL_TITLE = "detailTitle";
	//预留扩展字段
	public static final String PRODUCT_EXTR = "extr";
	
	
	public static final String[] commonColumns =
			new String[]{
				PRODUCT_ID,
				PRODUCT_NAME,
				PRODUCT_CATEGORY,
				PRODUCT_IS_COLLECTION,
				PRODUCT_TITLE,
				PRODUCT_FILE_SIZE,
				PRODUCT_VERSION,
				PRODUCT_DOWNLOAD_STATE,
				PRODUCT_PARENT,
				PRODUCT_LEVEL,
				PRODUCT_CHANGE_TIME,
				PRODUCT_OLD_PRICE,
				PRODUCT_PRICE_OF_DOLLAR,
				PRODUCT_PRICE_OF_GOLD,
				PRODUCT_DISCOUNT,
				PRODUCT_LONG_DESCRIPTION,
				PRODUCT_SHORT_DESCRIPTION,
				PRODUCT_HZ,
				PRODUCT_DETAIL_TITLE
			};
	
	public static final String CREATE_PRODUCT_TABLE
			= "CREATE TABLE IF NOT EXISTS "+ PRODUCT_TB_NAME + " ("
			+ PRODUCT_ID + "  text primary key, "
			+ PRODUCT_NAME + " text not null, "
			+ PRODUCT_CATEGORY + " text not null, "
			+ PRODUCT_IS_COLLECTION + " text, "
			+ PRODUCT_TITLE + " text not null, "
			+ PRODUCT_FILE_SIZE + " text, "
			+ PRODUCT_VERSION + " text, "
			+ PRODUCT_DOWNLOAD_STATE + " text, "
			+ PRODUCT_PARENT + " text, "
			+ PRODUCT_LEVEL + " text, "
			+ PRODUCT_CHANGE_TIME + " text, "
			+ PRODUCT_OLD_PRICE + " text, "
			+ PRODUCT_PRICE_OF_DOLLAR + " text, "
			+ PRODUCT_PRICE_OF_GOLD + " text, "
			+ PRODUCT_DISCOUNT + " text, "
			+ PRODUCT_LONG_DESCRIPTION + " text, "
			+ PRODUCT_SHORT_DESCRIPTION + " text, "
			+ PRODUCT_HZ + " text, "
			+ PRODUCT_DETAIL_TITLE + " text, "
			+ PRODUCT_EXTR + " text"		
			+ " );";
	
	//tapjoy table
	public static final String TAPJOY_TB_NAME = "TBTable";
	
	public static final String TAPJOY_KEY_ID = "_id";
	
	public static final String TAPJOY_ID = "ty_id";
	
	public static final String TAPJOY_NAME = "name";
	
	public static final String CREATE_TAPJOY_TABLE 
			= "CREATE TABLE IF NOT EXISTS " 
					+ TAPJOY_TB_NAME + " ("
					+ TAPJOY_KEY_ID + " integer primary key autoincrement, "
					+ TAPJOY_NAME + " text, " 
					+ TAPJOY_ID + " text)";
	
	// download task table
	public static final String DOWN_TASK_TB_NAME = "DownTaskTable";

	public static final String DOWN_TASK_KEY_ID = "_id";

	public static final String DOWN_TASK_NAME = "down_name";

	public static final String DOWN_TASK_STATE = "down_state";
	
	public static final String DOWN_TASK_LEVEL = "down_level";
	
	public static final String DOWN_TASK_CHANGE_TIME = "down_change_time";
	
	public static final String DOWN_FILE_SIZE = "down_file_size";
	//下载状态返回码
	public static final String DOWN_TASK_RSP_CODE = "down_task_rsp_code";
	
	public static final String CREATE_DOWNLOAD_TASK_TABLE 
		= "CREATE TABLE IF NOT EXISTS " 
			+ DOWN_TASK_TB_NAME + " ("
			+ DOWN_TASK_KEY_ID + " integer primary key autoincrement, "
			+ DOWN_TASK_NAME + " text, " 
			+ DOWN_TASK_LEVEL + " integer, "
			+ DOWN_TASK_CHANGE_TIME + " text, " 
			+ DOWN_TASK_STATE + " integer, "
			+ DOWN_FILE_SIZE + " integer, "
			+ DOWN_TASK_RSP_CODE + " integer"
			+ ")";
	
	public static final int DATABASE_VERSION = 1;
	
	private final Context mDbContext;
	private DBHelper mDBHelper = null;
	private SQLiteDatabase mDB = null;
	
	public static final Object lock = new Object();
	
	/**
	 * 
	 * @param c
	 */
	public DBUtil(Context c) {
		
		mDbContext = c;
		mDBHelper = new DBHelper(mDbContext);
	}
	
	/**
	 * Open a database
	 * @return
	 */
	public synchronized DBUtil openForRead() {
		
		mDB = mDBHelper.getReadableDatabase();	
		
		return this;
	}
	
	/**
	 * Open a database
	 * @return
	 */
	public synchronized DBUtil openForWrite() {
					
		mDB = mDBHelper.getWritableDatabase();

		return this;
	}
	
	/**
	 * Close a database
	 */
	public synchronized void closeDb() {
		
		if (null != mDBHelper) {
			
			mDBHelper.close();
		}
	}
	
	public void deleteDBAndRebuild() {
		
		if(mDB == null) {
			
			openForWrite();
		}
		
		if (null != mDbContext) {
			
			dropTable(DBUtil.PRODUCT_TB_NAME);			
			dropTable(DBUtil.TAPJOY_TB_NAME);
			dropTable(DOWN_TASK_TB_NAME);
			mDbContext.deleteDatabase(DATABASE_NAME);
			
			mDBHelper = new DBHelper(mDbContext);
		}
		
		if(mDB != null) {
			
			mDB.close();
		}
	}
	
	public void dropTable(String tableName) throws SQLException {

		if (null != mDB) {
			
			mDB.execSQL("DROP TABLE IF EXISTS " + tableName);
		}
	}
	
	public void createTable(String createTableSql) {
		
		if (null != mDB) {		
			mDB.execSQL(createTableSql);
		}
	}	
	
	public void updateDownInfo(String productId, String column, String value) {
		
		if(null == mDB){
			openForWrite();
		}
		
		ContentValues contentValues = new ContentValues();
		
		if(column.equals(DOWN_TASK_NAME)) {
			
			contentValues.put(DOWN_TASK_NAME, value);
		}else if (column.equals(DOWN_TASK_STATE)) {
			
			DebugUtil.debug(TAG, "write file status: " + value);
			
			contentValues.put(DOWN_TASK_STATE, Integer.parseInt(value));
		}else if (column.equals(DOWN_TASK_LEVEL)) {
			
			contentValues.put(DOWN_TASK_LEVEL, Integer.parseInt(value));
		}else if (column.equals(DOWN_TASK_CHANGE_TIME)) {
			
			contentValues.put(DOWN_TASK_CHANGE_TIME, value);
		}else if (column.equals(DOWN_FILE_SIZE)) {
			
			contentValues.put(DOWN_FILE_SIZE, Integer.parseInt(value));
		}else if(column.equals(DOWN_TASK_RSP_CODE)){
			
			contentValues.put(DOWN_TASK_RSP_CODE, Integer.parseInt(value));	
			return ;
		}
		
		mDB.update(DBUtil.DOWN_TASK_TB_NAME, 
				contentValues,
				DOWN_TASK_NAME + "=" 
				+  "'" + productId + "'",
				null
				);	
	}
	
	public void updateDownInfo(DownLoadTaskInfo info) {
		
		if(null == mDB){
			openForWrite();
		}
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(DOWN_TASK_NAME, info.getTaskName());
		contentValues.put(DOWN_TASK_STATE, info.getState());
		contentValues.put(DOWN_TASK_LEVEL, info.getLevel());
		contentValues.put(DOWN_TASK_CHANGE_TIME,
								info.getChangeTime());
		contentValues.put(DOWN_FILE_SIZE,
								info.getDownFileSize());
		contentValues.put(DOWN_TASK_RSP_CODE,
								info.getDownTaskRspCode());
		
		mDB.update(DBUtil.DOWN_TASK_TB_NAME, 
				contentValues,
				DOWN_TASK_NAME + "=" 
				+  "'" + info.getTaskName() + "'",
				null
				);	
	}
	public int getDownFileRspCode(String taskName) {
		
		int rspCode = 1;
		
		if(null == mDB){
			openForWrite();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
					new String[] { DOWN_TASK_RSP_CODE},
					DOWN_TASK_NAME + "=?",
					new String[] {taskName},
					null,
					null,
					null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			
			rspCode = cursor.getInt(0);
		}
		
		if(cursor != null) {
			
			cursor.close();
		}
		
		return rspCode;
	}
	
	public int getDownFileSize(String taskName) {
		
		int size = 1;
		
		if(null == mDB){
			openForWrite();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
					new String[] { DOWN_FILE_SIZE},
					DOWN_TASK_NAME + "=?",
					new String[] {taskName},
					null,
					null,
					null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			
			size = cursor.getInt(0);
		}
		
		if(cursor != null) {
			
			cursor.close();
		}
		
		return size;
	}
	public String getHighestFailedTask() {
		
		String taskName = null;
		
		if(null == mDB){
			openForWrite();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
					new String[] { DOWN_TASK_NAME},
					DOWN_TASK_STATE + "=?",
					new String[] { Constant.DOWNLOAD_STATE_FAILE},
					null,
					null,
					DOWN_TASK_LEVEL + " desc," 
					+ DOWN_TASK_CHANGE_TIME+ " desc");

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			
			taskName = cursor.getString(0);
		}
		
		if(cursor != null) {
			
			cursor.close();
		}
		
		return taskName;
	}
	
	public long insertNewDownTask(DownLoadTaskInfo info){
		
		if(null == mDB){
			openForWrite();
		}
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(DOWN_TASK_NAME, info.getTaskName());
		contentValues.put(DOWN_TASK_STATE, info.getState());
		contentValues.put(DOWN_TASK_LEVEL, info.getLevel());
		contentValues.put(DOWN_TASK_CHANGE_TIME,
								info.getChangeTime());
		contentValues.put(DOWN_FILE_SIZE,
								info.getDownFileSize());
		contentValues.put(DOWN_TASK_RSP_CODE,
								info.getDownTaskRspCode());
		
		long row = mDB.insert(DBUtil.DOWN_TASK_TB_NAME, "not input", contentValues);
		
		return row;
	}
	
	public int getDownTaskState(String taskName) {
		
		int downState = Constant.INT_DOWN_STATE_NOT_ENBLE;
		
		if(null == mDB){
			openForRead();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
				new String[] {DOWN_TASK_STATE},
				DOWN_TASK_NAME + "=?",
				new String[] {taskName},
				null,
				null,
				null);

		if (cursor != null && cursor.getCount() > 0) {
			
			cursor.moveToFirst();	
			downState = cursor.getInt(cursor.getColumnIndex(DOWN_TASK_STATE));
		}

		if (cursor != null) {
			cursor.close();
		}
		
		return downState;
	}
	
	
	public String getHighestLevelDownTask() {
		
		String taskName = null;
		
		if(null == mDB){
			openForRead();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
				new String[] {DOWN_TASK_NAME},
				DOWN_TASK_STATE + "=?",
				new String[] {Constant.DOWNLOAD_STATE_START},
				null,
				null,
				DOWN_TASK_LEVEL + " desc," 
				+ DOWN_TASK_CHANGE_TIME  + " desc");

		if (cursor != null && cursor.getCount() > 0) {
			
			cursor.moveToFirst();	
			taskName = cursor.getString(0);
		}

		if (cursor != null) {
			cursor.close();
		}
		
		return taskName;
	}
	
	public String getProductDownloadState(String productId) {
		
		String downloadState = Constant.DOWNLOAD_STATE_NOT_ENBLE;
		
		if(mDB == null)  {
			
			openForRead();
		}
		
		Cursor cursor 
			= mDB.query(PRODUCT_TB_NAME, 
					new String[]{PRODUCT_DOWNLOAD_STATE}, 
					PRODUCT_NAME + "=?", 
					new String[]{productId},
					null, 
					null, 
					null);
		
		if(cursor.moveToFirst()) {
			
			downloadState = cursor.getString(
					cursor.getColumnIndex(
							PRODUCT_DOWNLOAD_STATE));
		}
		
		if(cursor != null) {
			
			cursor.close();
		}
		
		return downloadState;
	}
	
	public String getTapJoyIdByProductId(String productId) {
		
		String tapjoyId = "";
		
		if(mDB == null)  {
			
			openForRead();
		}
		
		Cursor cursor 
			= mDB.query(TAPJOY_TB_NAME, 
					new String[]{TAPJOY_ID}, 
					TAPJOY_NAME + "=?", 
					new String[]{productId},
					null, 
					null, 
					null);
		
		if(cursor.moveToFirst()) {
			
			tapjoyId = cursor.getString(
					cursor.getColumnIndex(
							TAPJOY_ID));
		}
		
		if(cursor != null) {
			
			cursor.close();
		}
		
		return tapjoyId;
	}
	
	public long insertProduct(ProductInfo productInfo){	
		
		ContentValues initialValues = putDataToContentValues(productInfo);
		
		if(null == mDB){
			openForWrite();
		}
		long row = mDB.insert(PRODUCT_TB_NAME, "not input", initialValues);
		
		return row;	
	}
	
	public long insertTapJoyData(TapJoyInfo info) {
		
		if(null == mDB){
			openForWrite();
		}
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(DBUtil.TAPJOY_ID, info.getId());
		contentValues.put(DBUtil.TAPJOY_NAME, info.getName());
		
		long row = mDB.insert(DBUtil.TAPJOY_TB_NAME, "not input", contentValues);
		
		return row;
	}
	
	public ContentValues putDataToContentValues(ProductInfo productInfo){
		
		ContentValues initialValues = new ContentValues();	
		initialValues.clear();
		
		initialValues.put(PRODUCT_ID, productInfo.getId());
		initialValues.put(PRODUCT_NAME, productInfo.getName());
		initialValues.put(PRODUCT_CATEGORY, productInfo.getCategory());
		initialValues.put(PRODUCT_TITLE, productInfo.getTitle());
		initialValues.put(PRODUCT_FILE_SIZE, productInfo.getSize());
		initialValues.put(PRODUCT_VERSION, productInfo.getVersion());
		initialValues.put(PRODUCT_DOWNLOAD_STATE, productInfo.getDownloadState());
		initialValues.put(PRODUCT_PARENT, productInfo.getParentId());
		initialValues.put(PRODUCT_CHANGE_TIME, productInfo.getChangeTime());
		initialValues.put(PRODUCT_LEVEL, productInfo.getLevel());	
		initialValues.put(PRODUCT_OLD_PRICE, productInfo.getOriginalPrice());
		initialValues.put(PRODUCT_PRICE_OF_DOLLAR, productInfo.getPriceOfDollar());
		initialValues.put(PRODUCT_PRICE_OF_GOLD, productInfo.getPriceOfGold());
		initialValues.put(PRODUCT_DISCOUNT, productInfo.getDiscount());		
		initialValues.put(PRODUCT_LONG_DESCRIPTION, productInfo.getLongDescription());
		initialValues.put(PRODUCT_SHORT_DESCRIPTION, productInfo.getShortDecription());
		initialValues.put(PRODUCT_HZ, productInfo.getHzOfProduct());
		initialValues.put(PRODUCT_DETAIL_TITLE, productInfo.getDetailTitle());
		String isCollection = "false";
		
		if (productInfo.isProductCollection()) {
			
			isCollection = "true";
		}
		
		initialValues.put(PRODUCT_IS_COLLECTION, isCollection);
		
		return initialValues;
	}
	
	public boolean updateProduct(ProductInfo productInfo) {
		
		ContentValues initialValues = putDataToContentValues(productInfo);
		
		if(null == mDB) {
			
			openForWrite();
		}
		
		boolean isUpdateSuccess = mDB.update(PRODUCT_TB_NAME, initialValues, 
				PRODUCT_ID + "=" +  "'" + productInfo.getId() + "'", null) > 0;
		
		return isUpdateSuccess;
	}
	
	public void loadUserLibProduct(Store store, User user) {
		
		if(null == mDB) {
			
			openForRead();
		}
		
		Cursor cursor = mDB.query(
				DOWN_TASK_TB_NAME,
				new String[] {DOWN_TASK_NAME},
				DOWN_TASK_NAME + " Like " + "'%.music'"
						+ " And " + DOWN_TASK_STATE + "!=?",
				new String[] {Constant.DOWNLOAD_STATE_NOT_ENBLE},
				null, null,
				DOWN_TASK_CHANGE_TIME + " desc");
				
		while (cursor.moveToNext()) {
			
			String productName = cursor.getString(0).replace(".music", "");
			Product product = store.getProductByName(productName);
			user.getLocalLib().addProduct(product);
			
			DebugUtil.debug(TAG, "add user local product:" + productName);
		};
		
		if(cursor != null) {
			
			cursor.close();
		}	
	}
	
	//从数据库读取产品
	public boolean loadAllProductsFromDB(Store store,User user) {
		
		boolean isLoadSuccess = false;
		
		ArrayList<Product> products = new ArrayList<Product>();
		
		if(null == mDB) {
			
			openForRead();
		}
		Cursor cursor = mDB.query(PRODUCT_TB_NAME, commonColumns, 
				null, 
				null,
				null, null, null);	
		
		while(cursor.moveToNext()) {
			
			isLoadSuccess = true;
			DebugUtil.dbDebug("database", "load data form database");
			
			ProductInfo info = new ProductInfo();
			
			info.setCategory(cursor.getString(
					cursor.getColumnIndex(PRODUCT_CATEGORY)));
			info.setChangeTime(cursor.getString(
					cursor.getColumnIndex(PRODUCT_CHANGE_TIME)));
			info.setDiscount(cursor.getString(
					cursor.getColumnIndex(PRODUCT_DISCOUNT)));
			info.setDownloadState(cursor.getString(
					cursor.getColumnIndex(PRODUCT_DOWNLOAD_STATE)));
			info.setId(cursor.getString(
					cursor.getColumnIndex(PRODUCT_ID)));
			info.setIsProductCollection(
					cursor.getString(cursor.getColumnIndex(PRODUCT_IS_COLLECTION))
					.equals("true"));
			info.setLevel(cursor.getString(cursor.getColumnIndex(PRODUCT_LEVEL)));
			info.setName(
					cursor.getString(cursor.getColumnIndex(PRODUCT_NAME)));
			info.setOriginalPrice(cursor.getString(
					cursor.getColumnIndex(PRODUCT_OLD_PRICE)));
			info.setParentId(cursor.getString(
					cursor.getColumnIndex(PRODUCT_PARENT)));
			info.setPriceOfDollar(cursor.getString(
					cursor.getColumnIndex(PRODUCT_PRICE_OF_DOLLAR)));
			info.setSize(
					cursor.getString(cursor.getColumnIndex(PRODUCT_FILE_SIZE)));
			info.setTitle(
					cursor.getString(cursor.getColumnIndex(PRODUCT_TITLE)));
			info.setVersion(
					cursor.getString(cursor.getColumnIndex(PRODUCT_VERSION)));
			info.setPriceOfGold(
					cursor.getString(cursor.getColumnIndex(PRODUCT_PRICE_OF_GOLD)));
			info.setLongDescription(
					cursor.getString(cursor.getColumnIndex(PRODUCT_LONG_DESCRIPTION)));
			info.setShortDescription(
					cursor.getString(cursor.getColumnIndex(PRODUCT_SHORT_DESCRIPTION)));
			info.setHzOfProduct(
					cursor.getString(cursor.getColumnIndex(PRODUCT_HZ)));	
			info.setDetailTitle(
					cursor.getString(cursor.getColumnIndex(PRODUCT_DETAIL_TITLE)));
			DebugUtil.debug(TAG, "readoutname:" + info.getName());
			DebugUtil.debug(TAG, "readouttitle:" + info.getTitle());
			DebugUtil.debug(TAG, "readoutState:" + info.getDownloadState());
			
			Product product = null;
			
			if(info.isProductCollection()) {
				
				String collectionName = info.getName();
				
				product = store.getProductByName(collectionName);
				
				if(null == product) {
					//合集不在商店产品列表中，重新生成一个合集对象，然后添加到商店产品列表中
					product = new WaveCollection();
					store.addProduct(collectionName, product);
				}
				//将合集对象添加到商店的分类列表中
				store.addProductToCategory(info.getCategory(), product);				
			}else {
				
				product = new Wave();
				
				//添加该商品到商店产品列表中
				store.addProduct(info.getName(), product);
				
				String parentId = info.getParentId();
				
				if(!info.getName().equals(parentId)) {
					//该产品是某一个合集下面的产品，从商店读取合集产品信息
					Product waveCollection = store.getProductByName(parentId);
					//商店产品信息中没有合集产品
					if(waveCollection == null) {
						//生成合集产品，并加入商店，具体的合集信息，在数据库解析到该合集时，完善
						waveCollection = new WaveCollection();
						//添加商品到合集中
						waveCollection.addProduct(product);
						//此商品是合集中的某一个产品，添加合集产品到商店产品列表中，但是不添加到分类列表中
						store.addProduct(parentId, waveCollection);
					}else {
						
						waveCollection.addProduct(product);
					}
					
				}else {
					//添加单个产品到分类列表
					store.addProductToCategory(info.getCategory(), product);
				}
			}
			
			product.setProductInfo(info);
			
			if(info.getDownloadState().equals(Constant.PRODUCT_STATE_FREE)) {
				
				user.getLocalLib().addProduct(product);
			}
		}
		
		if(null != cursor){
			
			cursor.close();
		}
		
		return isLoadSuccess;
	}
}
